/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sendtion.xrichtextdemo.adapter;

import com.bumptech.glide.Glide;
import com.sendtion.xrichtextdemo.ResourceTable;
import com.sendtion.xrichtextdemo.utils.DialogUtil;
import com.zhengsr.viewpagerlib.callback.OnImageScaleListener;
import com.zhengsr.viewpagerlib.view.ScaleImageView;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderProvider;
import ohos.app.Context;
import ohos.utils.net.Uri;

import java.util.List;

/**
 * MyPagerProvider2
 *
 * @since 2021-06-22
 */
public class MyPagerProvider2 extends PageSliderProvider implements OnImageScaleListener {
    private List<Uri> data;

    private Context context;

    private PageSlider pageSlider;

    private DialogUtil.ItemClick click;

    /**
     * 构造
     *
     * @param context
     * @param data
     * @param pageSlider
     * @param click
     */
    public MyPagerProvider2(Context context, List<Uri> data, PageSlider pageSlider, DialogUtil.ItemClick click) {
        this.data = data;
        this.context = context;
        this.pageSlider = pageSlider;
        this.click = click;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        DependentLayout layout = (DependentLayout) LayoutScatter.getInstance(context).parse(
                ResourceTable.Layout_item_zoom_banner, null, false);
        ScaleImageView image = (ScaleImageView) layout.findComponentById(ResourceTable.Id_image);
        image.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                click.onClick();
            }
        });
        image.setOnImageScaleListener(this);
        Glide.with(context).load(data.get(i)).into(image);
        componentContainer.addComponent(layout);
        return layout;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return false;
    }

    @Override
    public void onImageScale(ScaleImageView scaleImageView) {
        if (scaleImageView.isScale()) {
            pageSlider.setSlidingPossible(false);
        } else {
            pageSlider.setSlidingPossible(true);
        }
    }
}
